package com.example.service.impl;

import com.example.common.CaptureConfig;
import com.example.common.CustomException;
import com.example.dao.AdminDao;
import com.example.dao.CategoryDao;
import com.example.entity.Admin;
import com.example.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 管理员信息服务实现类
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminDao adminDao;
    
    @Resource
    private CategoryDao categoryDao;

    @Override
    public Admin login(Admin user, HttpServletRequest request, HashMap map) {
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        map.put("role", user.getRole());
        String key = request.getParameter("key");
        
        // 判断验证码
        if(!user.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))){
            throw new CustomException("验证码错误");
        }
        
        List<Admin> list = adminDao.selectLogin(map);
        if(list.size() == 0){
            throw new CustomException("用户名或密码错误");
        } else {
            request.getSession().setAttribute("admin", list.get(0));
        }
        
        // 登录成功后清除正确的验证码
        CaptureConfig.CAPTURE_MAP.remove(key);
        
        // 获取当前登录用户信息
        return (Admin) request.getSession().getAttribute("admin");
    }

    @Override
    public void register(Admin admin, HttpServletRequest request, HashMap map) {
        map.put("username", admin.getUsername());
        List<Admin> list = adminDao.selectOne(map);
        
        // 判断用户名的唯一性
        if (list.size() > 0) {
            throw new CustomException("该用户名已存在，请更换");
        }
        
        adminDao.add(admin);
    }

    @Override
    public PageInfo<Admin> getAdminList(Integer pageNum, Integer pageSize, String key, String key1, String key2, String role) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("key1", key1);
        map.put("key2", key2);
        map.put("role", role);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminDao.selectAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Admin admin, HttpServletRequest request) {
        if (admin.getId() == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("username", admin.getUsername());
            List<Admin> list = adminDao.selectOne(map);
            
            // 判断用户名的唯一性
            if (list.size() > 0) {
                throw new CustomException("该用户名已存在，请更换");
            }
            
            adminDao.add(admin);
        } else {
            adminDao.update(admin);
        }
    }

    @Override
    public void updatePassword(Admin admin, String onlpassword, String confirmPasword) {
        if (!onlpassword.equals(admin.getPassword())) {
            throw new CustomException("原密码输入错误");
        }
        
        admin.setPassword(confirmPasword);
        adminDao.update(admin);
    }

    @Override
    public void deleteAdmin(int id) {
        adminDao.delete(id);
    }

    @Override
    public void batchDeleteAdmin(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            adminDao.delete(Integer.parseInt(id));
        }
    }
} 